<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\Admin\UserRoleRequest;
use App\Models\UserRole;
use App\Traits\Admin\SuccessOrFailure;
use Daijulong\LaravelRoles\Permission;
use Illuminate\Http\Request;

class UserRoleController extends Controller
{

    use SuccessOrFailure;

    private $permission = 'role.user';


    public function index()
    {
        $this->canOrFailure($this->permission);
        $roles = UserRole::query()->orderBy('id')->get();
        return view('admin.role.user.index', compact('roles'));
    }

    public function create()
    {
        $this->canOrFailure($this->permission);
        return view('admin.role.user.create');
    }

    public function store(UserRoleRequest $request)
    {
        $this->canOrFailure($this->permission);
        $attributes = $this->buildData($request);
        UserRole::create($attributes);
        return $this->success('', route('admin.role.user.index'));
    }

    public function edit($id)
    {
        $this->canOrFailure($this->permission);
        $role = $this->getRoleOrFailure($id);
        return view('admin.role.user.edit', compact('role'));
    }

    public function update(UserRoleRequest $request, $id)
    {
        $this->canOrFailure($this->permission);
        $role = $this->getRoleOrFailure($id);
        $attributes = $this->buildData($request);
        $role->update($attributes);
        return $this->success('', $request->get('back_url') ?: route('admin.role.user.index'));
    }

    public function buildData($request)
    {
        return [
            'name' => $request->get('name'),
            'alias' => $request->get('alias'),
            'description' => $request->get('description'),
        ];
    }

    public function destroy($id)
    {
        $this->canOrFailure($this->permission);
        $role = $this->getRoleOrFailure($id);
        $users_count = $role->users()->count();
        if ($users_count > 0) {
            return $this->failure('有' . $users_count . '位用户使用此用户组，请处理后再删除！');
        }
        $role->delete();
        return $this->success();
    }

    public function showAuthorizeForm($id)
    {
        $this->canOrFailure($this->permission);
        $role = $this->getRoleOrFailure($id);
        $permissions = Permission::getModelPermissions('user');
        return view('admin.role.user.authorize', compact('role', 'permissions'));
    }

    public function saveAuthorization(Request $request, $id)
    {
        $this->canOrFailure($this->permission);

        $role = $this->getRoleOrFailure($id);
        $permissions = $request->get('permissions', []);
        $role->update(['permissions' => $permissions]);
        return $this->success('', $request->get('back_url') ?: route('admin.role.user.index'));
    }

    private function getRoleOrFailure($id)
    {
        $role = UserRole::find($id);
        if (!$role) {
            response($this->failure('用户组不存在！'), 404)->send();
            exit;
        }
        return $role;
    }
}
